package com.project.util.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class CannotContainSpacesValidator implements ConstraintValidator<CannotContainSpaces,String> {
    private int len;
    @Override
    public void initialize(CannotContainSpaces cannotContainSpaces) {
        this.len = cannotContainSpaces.length();
    }

    @Override
    public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext) {
        if (str != null) {
            if (str.indexOf(" ") < 0) {
                return true;
            }
        }else {
            constraintValidatorContext.disableDefaultConstraintViolation();//禁用默认的message的值
            //重新添加错误提示语句
            constraintValidatorContext.buildConstraintViolationWithTemplate("字符串不能为空").addConstraintViolation();
        }
        return false;
    }
}
